
package com.huaxindata.im.main.chat.listener;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.huaxindata.im.R;
import com.huaxindata.im.main.chat.Helper.FileHelper;
import com.huaxindata.im.main.chat.adapter.ChatAdapter;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatVoiceMessageBody;

import java.io.File;

public class VoicePlayClickListener implements View.OnClickListener {
    private static final String TAG = "VoicePlayClickListener";
    private final ChatVoiceMessageBody messageBody;
    ChatMessage message;
    ImageView voiceIconView;

    private AnimationDrawable voiceAnimation = null;
    MediaPlayer mediaPlayer = null;

    Context mContext;
    private ChatAdapter adapter;

    ChatAdapter.ViewHolder holder;

    public static boolean isPlaying = false;
    public static VoicePlayClickListener currentPlayListener = null;

    public VoicePlayClickListener(ChatMessage message, ImageView v, ChatAdapter adapter, Context context, ChatAdapter.ViewHolder holder) {
        this.message = message;
        this.holder = holder;
        this.adapter = adapter;
        voiceIconView = v;
        this.mContext = context;
        this.messageBody=(ChatVoiceMessageBody)message.getMessageBody();
    }

    public void stopPlayVoice() {
        voiceAnimation.stop();
        String clientId= UserManager.getClientId();
        if (!clientId.equals(message.sender)) {
            voiceIconView.setImageResource(R.mipmap.chatfrom_voice_playing);
        } else {
            voiceIconView.setImageResource(R.mipmap.chatto_voice_playing);
        }
        // stop play voice
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }

        isPlaying = false;
        messageBody.isPlay=false;
       // ChatAdapter.playMsgId = -1;
        voiceIconView.invalidate();
    }

    public void playVoice(String filePath) {
        if (!(new File(filePath).exists())) {
            return;
        }
      //  ChatAdapter.playMsgId = message.time;

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepare();
            isPlaying = true;
            messageBody.isPlay=true;
            currentPlayListener = this;
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    mediaPlayer.release();
                    mediaPlayer = null;
                    stopPlayVoice(); // stop animation
                }

            });
            showAnimation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // show the voice playing animation
    @SuppressWarnings("ResourceType")
    private void showAnimation() {
        // play voice, and start animation
        if (!message.isSender(UserManager.getClientId())) {
            voiceIconView.setImageResource(R.drawable.voice_from_icon);
        } else {
            voiceIconView.setImageResource(R.drawable.voice_to_icon);
        }
        voiceAnimation = (AnimationDrawable) voiceIconView.getDrawable();
        voiceAnimation.start();
    }

    @Override
    public void onClick(View v) {
        if (isPlaying) {
            currentPlayListener.stopPlayVoice();
//            if (ChatAdapter.playMsgId == message.time) {
//                return;
//            }
            return;
        }

        File file = new File(messageBody.getLocalPath());
        if (file != null && file.exists()) {
            playVoice(file.getAbsolutePath());
        } else{
            messageBody.downloadStatus = 1;
            FileHelper.downLoadFileMessage(message,mContext,adapter);
            Toast.makeText(mContext, "正在下载,请稍等", Toast.LENGTH_SHORT).show();
        }
    }
}